package priv.ln.project;

/**
 * ClassName: CustomerView
 * Package: priv.ln.project
 * Description:
 * 主模块，负责菜单的显示和处理用户操作
 * CustomerList customerList = new CustomerList(10);
 * 创建最大包含10个客户对象的CustomerList 对象，供以下各成员方法使用。
 * 该类至少提供以下方法：
 * public void enterMainMenu()
 * private void addNewCustomer()
 * private void modifyCustomer()
 * private void deleteCustomer()
 * private void listAllCustomers()
 * public static void main(String[] args)
 * @Author 辣辣
 * @Create 2024/6/27 16:07
 * @Version 1.0
 */
public class CustomerView {
    CustomerList customerList = new CustomerList(5);

    /**
     * 显示主菜单，响应用户输入，根据用户操作分别调用其他相应的成员方法（如addNewCustomer），
     * 以完成客户信息处理。
     */
    public void enterMainMenu(){

        boolean isExit = false;
        while(!isExit){
            System.out.println("------------拼电商客户管理系统------------");
            System.out.println();
            System.out.println("               1 添加客户               ");
            System.out.println("               2 修改客户               ");
            System.out.println("               3 删除客户               ");
            System.out.println("               4 客户列表               ");
            System.out.println("               5 退   出               ");
            System.out.println();
            System.out.print("              请选择（1-5）:");
            int selection = Utility.readMenuSelection();
            switch(selection) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    isExit = true;
                    break;
            }
        }

    }

    /**
     * 添加客户
     */
    private void addNewCustomer(){
        System.out.println("------------添加客户------------");
        System.out.print("姓名：");
        String name = Utility.readString(10);
        System.out.print("性别：");
        char gender = Utility.readChar();
        System.out.print("年龄：");
        int age = Utility.readInt();
        System.out.print("电话：");
        String phone = Utility.readString(20);
        System.out.print("邮箱：");
        String email = Utility.readString(30);
        boolean isCompleted = customerList.addCustomer(new Customer(name, gender, age, phone, email));
        if(isCompleted){
            System.out.println("------------添加完成------------");
        }else {
            System.out.println("------------添加失败------------");
        }
    }


    /**
     * 修改客户
     */
    private void modifyCustomer(){
        System.out.println("------------修改客户------------");
        int num;
        Customer cus;
        for (;;){
            System.out.print("请选择待修改客户编号（-1退出）：");
            num = Utility.readInt();
            if(num == -1){
                return;
            }
            cus = customerList.getCustomer(num - 1);
            if(cus != null) {
                break;
            }
        }

        System.out.println("姓名" + cus.getName() + ":");
        String name = Utility.readString(10,cus.getName());
        System.out.println("性别" + cus.getGender() + ":");
        char gender = Utility.readChar(cus.getGender());
        System.out.println("年龄" + cus.getAge() + ":");
        int age = Utility.readInt(cus.getAge());
        System.out.println("电话" + cus.getPhone() + ":");
        String phone = Utility.readString(20,cus.getPhone());
        System.out.println("邮箱" + cus.getEmail() + ":");
        String email = Utility.readString(30,cus.getEmail());
        cus = new Customer(name, gender, age, phone, email);
        boolean isReplaced = customerList.replaceCustomer(num - 1,cus);
        if(isReplaced){
            System.out.println("------------修改完成------------");
        }else {
            System.out.println("------------修改失败------------");
        }

    }

    private void deleteCustomer(){
        System.out.println("------------删除客户------------");
        int num;
        Customer cus;
        for (;;){
            System.out.print("请选择待删除客户编号（-1退出）：");
            num = Utility.readInt();
            if(num == -1){
                return;
            }
            cus = customerList.getCustomer(num - 1);
            if(cus != null) {
                break;
            }
        }
            System.out.print("确认是否删除：（Y/N）：");
            char choice = Utility.readConfirmSelection();
            if(choice == 'Y'){
                customerList.deleteCustomer(num - 1);
                System.out.println("------------删除完成------------");
            }else {
                System.out.println("------------删除失败------------");
            }


    }
    private void listAllCustomers(){
        System.out.println("------------客户列表------------");
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
        Customer[] custs = customerList.getAllCustomers();
        for (int i = 0; i < custs.length; i++) {
            System.out.println(i+1 + "\t" + custs[i].getName() + "\t" +
                    custs[i].getGender() + "\t" + custs[i].getAge() + "\t"
                    + custs[i].getPhone() + "\t" + custs[i].getEmail());
        }
        System.out.println("----------客户列表完成----------");
    }

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();

    }


}
